from selenium import webdriver
from selenium.webdriver.edge.options import Options
import numpy as np
import os
import time

# "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9222 --user-data-dir="C:\EdgeDebugProfile"

def download_etopo_tiles():
    # 要下载的URL列表
    url00 = 'https://www.ngdc.noaa.gov/mgg/global/relief/ETOPO2022/data/15s/15s_surface_elev_gtif/ETOPO_2022_v1_15s_S30E030_surface.tif'
    url01 = 'https://www.ngdc.noaa.gov/mgg/global/relief/ETOPO2022/data/15s/15s_surface_elev_gtif/ETOPO_2022_v1_15s_S45E030_surface.tif'
    url02 = 'https://www.ngdc.noaa.gov/mgg/global/relief/ETOPO2022/data/15s/15s_surface_elev_gtif/ETOPO_2022_v1_15s_S60E030_surface.tif'

    download_directory = r"D:\ETOPO2022"
    urls_not_available_directory = r"D:\ETOPO2022NOT_AVAILABLE"

    not_available_name = r'not_available.txt'

    if not os.path.exists(urls_not_available_directory):
        os.makedirs(urls_not_available_directory)

    file_names = os.listdir(download_directory)
    print(f"Found {len(file_names)} files.")
    downloading = False

    for file_name in file_names:
        if file_name.endswith(".crdownload"):
            downloading = True

    if downloading:
        print("Downloading cannot proceed.")
        return

    urls_all = []
    urls_not_available = []

    interval = 15
    lons = np.arange(-180, 180 + interval, interval)
    lats = np.arange(-90, 90 + interval, interval)

    for lon in lons:
        lon_str = f'E{str(abs(lon)).zfill(3)}'
        if lon < 0: lon_str = f'W{str(abs(lon)).zfill(3)}'
        for lat in lats:
            lat_str = f'N{str(abs(lat)).zfill(2)}'
            if lat < 0: lat_str = f'S{str(abs(lat)).zfill(2)}'
            file_name = f'ETOPO_2022_v1_15s_{lat_str}{lon_str}_surface.tif'
            url = f'https://www.ngdc.noaa.gov/mgg/global/relief/ETOPO2022/data/15s/15s_surface_elev_gtif/{file_name}'
            if file_name not in file_names: urls_all.append(url)

    not_available_txt_url = os.path.join(urls_not_available_directory, not_available_name)
    not_available_txt_url_exists = os.path.exists(not_available_txt_url)
    if not_available_txt_url_exists:
        with open(not_available_txt_url, 'r', encoding='utf-8') as file:
            content = file.read()
            urls_not_available = content.split('\n')

    urls_all = list(set(urls_all) - set(urls_not_available))
    # 若路径不存在，在本地新建这个目录
    if not os.path.exists(download_directory):
        os.makedirs(download_directory)

    download_count = 0
    download_available_count = 0
    download_total = 8

    # 配置Edge选项，连接到已打开的浏览器
    edge_options = Options()
    edge_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    edge_options.add_experimental_option("prefs", {
        "download.default_directory": download_directory,
        "download.prompt_for_download": False,
    })
    # edge_options.add_experimental_option('detach', True)
    # 连接到已运行的Edge实例
    driver = webdriver.Edge(options=edge_options)

    try:
        # 批量访问URL实现下载（浏览器会自动处理下载）
        while download_count < download_total:
            if download_count >= len(urls_all):
                continue
            driver.get(urls_all[download_count])
            print(f"已触发下载: {urls_all[download_count]}")

            page_status = ''
            try:
                page_status = driver.page_source # 返回字符串格式的 HTML 源码
            except:
                print('File existed.')
            finally:
                if (type(page_status) == str and page_status.find('404') != -1 and
                        driver.current_url.find('www.ngdc.noaa.gov') != -1):
                    print(f"{driver.current_url} does not exist.")
                    urls_not_available.append(driver.current_url)
                # 等待下载开始（根据网络情况调整时间）
                time.sleep(2)
                download_count += 1
    finally:
        with open(not_available_txt_url, 'w', encoding='utf-8') as file:
            file.write('\n'.join(list(set(urls_not_available))))
        # 保持浏览器打开（如果需要自动关闭可改为driver.quit()）
        pass

if __name__ == "__main__":
    download_etopo_tiles()